﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.WebUI.Templates.WinnerCMS6.Member
{
    public partial class Favorite : WebFactory.ClientPublicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.Title = WebConfig.SiteName + "→ 会员中心 → 网络收藏夹";

                BindList(1);
            }
        }

        private void BindList(int pageNo)
        {
            this.GV_List.EmptyDataTemplate = WebFactory.PageBase.GetTemplate(this);

            var pp = new PagingParam();
            pp.PageSize = this.PageNavigation1.PageSize;
            pp.PageNo = pageNo;

            this.GV_List.DataSource = new FavoriteManage().BindList(pp);
            this.GV_List.DataBind();

            this.PageNavigation1.RecordCount = pp.RecordCount;
        }

        #region 添加或修改

        private void Add(int ID, string Title, string URL)
        {
            var model = new Model.Favorite();
            model.Id = ID;
            model.Title = Title;
            model.Url = URL;
            model.UserId = UserManage.Current.UserID;

            var res = new FavoriteManage().Add(model);

            if (string.IsNullOrEmpty(res.Errors) == false)
            {
                MsgBox.Message(res.Errors, this);
            }
        }

        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Add(0, this.txtTitle.Text.Trim(), this.txtURL.Text.Trim());

            BindList(1);
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var myTableCell = e.Row.Cells[4];
                if (myTableCell.Controls.Count > 0)
                {
                    var btnDel = (LinkButton) myTableCell.Controls[0];

                    btnDel.Attributes.Add("onclick", "return confirm('您确定要删除？');");
                }
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = this.GV_List.DataKeys[e.RowIndex].Value.ToString();

            new FavoriteManage().Delete(id, this.UpdatePanel1);


            MsgBox.Message("删除成功!", this.UpdatePanel1);

            BindList(1);
        }

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var id = (int) this.GV_List.DataKeys[e.RowIndex].Value;
            var Title = ((TextBox) this.GV_List.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim();
            var URL = ((TextBox) this.GV_List.Rows[e.RowIndex].Cells[1].Controls[1]).Text.Trim();
            Add(id, Title, URL);

            this.GV_List.EditIndex = -1;
            BindList(1);
        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GV_List.EditIndex = -1;
            BindList(1);
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GV_List.EditIndex = e.NewEditIndex;
            BindList(1);
        }

        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }
    }
}